package com.zlf.mapper;

import com.zlf.po.PointTransaction;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface PointTransactionMapper {

    @Insert("insert into pointtransaction " +
            "(pt_id, user_id,value, create_time, expired_time, state) " +
            "values " +
            "(#{ptId}, #{userId}, #{value}, #{createTime}, #{expiredTime}, #{state})")
    @Options(useGeneratedKeys = true,keyProperty = "ptId",keyColumn = "pt_id")
    void addTrans(PointTransaction transaction);

    @Insert("insert into pointtransaction " +
            "(pt_id, user_id,value, create_time, expired_time, state) " +
            "values " +
            "(#{ptId}, #{userId}, #{value}, #{createTime}, #{expiredTime},null)")
    @Options(useGeneratedKeys = true,keyProperty = "ptId",keyColumn = "pt_id")
    void exTrans(PointTransaction pointTransaction);
    @Select("select pt_id, user_id, value, state, create_time, expired_time " +
            "from pointtransaction where user_id = #{userId} and state IS NULL")
    List<PointTransaction> getExchangeTrans(String userId);

    @Select("select pt_id, user_id, value, state, create_time, expired_time " +
            "from pointtransaction where user_id = #{userId} and value IS NOT NULL")
    List<PointTransaction> getIncomeTrans(String userId);

    @Select("select pt_id,user_id, value, state, create_time, expired_time " +
            "from pointtransaction where user_id = #{userId}")
    List<PointTransaction> getAllTrans(String userId);
//    @Select("select pt_id, user_id, state, expired_time " +
//            "from pointtransaction " +
//            "where user_id = #{userId}  and state is not null and state > 0 and expired_time > #{now} " +
//            "order by expired_time")
    List<PointTransaction> getValidTrans(@Param("userId") String userId, @Param("now") long now);
    @Update("update pointtransaction set state = #{value} where pt_id = #{ptId}")
    void updateState(@Param("value")Integer value, @Param("ptId")Integer ptId);

    @Delete("delete from pointtransaction where state = -1 OR state=0")
    void delExpiredTrans();

    @Update("update pointtransaction set state = -1 where expired_time < #{now}")
    void setTransExpired(long now);

}
